package com.yundasys.lab08jvm.heap;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @PACKAGE_NAME: com.yundasys.lab08jvm.heap
 * @NAME: MetaspaceOutOfMemoryDemo
 * @USER: jiangta
 * @DATE: 2021/7/6
 * @TIME: 9:12 上午
 * @PROJECT_NAME: springboot-labs
 */
public class MetaspaceOutOfMemoryDemo {

    // 静态类
    static class OOMTest {

    }

    public static void main(String[] args) {
        // 模拟计数多少次以后发生异常
        int i =0;
        try {
            while (true) {
                i++;
                // 使用Spring的动态字节码技术
                Enhancer enhancer = new Enhancer();
                enhancer.setSuperclass(OOMTest.class);
                enhancer.setUseCache(false);
                enhancer.setCallback(new MethodInterceptor() {
                    @Override
                    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
                        return methodProxy.invokeSuper(o, args);
                    }
                });
            }
        } catch (Exception e) {
            System.out.println("发生异常的次数:" + i);
            e.printStackTrace();
        } finally {

            //StackoverFlowError 栈溢出
            //OutOfMemoryError：java heap space 堆溢出

            //java.lang.StackOverflowError
            //java.lang.OutOfMemoryError:java heap size
            //java.lang.OutOfMemroyError:GC overhead limit exceeded
            //java.lang.OutOfMemoryError:Direct buffer memory
            //java.lang.OutOfMemoryError:unable to create new native thread
            //java.lang.OutOfMemroyError:Metaspace
        }
    }
}
